package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.db.UserApi;
import com.tanhua.dubbo.api.mapper.UserMapper;
import com.tanhua.model.db.User;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 用户服务接口实现类
 */
@DubboService //发布服务
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    /**
     * 根据手机号查询用户对象
     *
     * @param phone
     * @return
     */
    @Override
    public User findByMobile(String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",phone);
        return userMapper.selectOne(queryWrapper);
    }


    /**
     * 注册用户返回用户id
     *
     * @param user
     * @return
     */
    @Override
    public Long add(User user) {
        /*Date nowDate = new Date();
        user.setCreated(nowDate);
        user.setUpdated(nowDate);*/
        userMapper.insert(user);
        return user.getId();
    }

    /**
     * 根据用户id更新 环信账号（用户ID） 密码固定123456
     * @param huanxUser
     */
    @Override
    public void update(User huanxUser) {
        userMapper.updateById(huanxUser);
    }

    /**
     * 根据用户id查询用户对象
     */
    @Override
    public User findById(Long userId) {
        return userMapper.selectById(userId);
    }

    /**
     * 环信ID查询用户信息
     */
    @Override
    public User findByHuanxinUser(String huanxinId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hx_user",huanxinId);
        return userMapper.selectOne(queryWrapper);
    }
}
